<template>
  <div class="app-wrapper" :class="{ 'mobile': device === 'mobile' }">
    <!-- 侧边栏 -->
    <div class="sidebar-container" :class="{ 'is-collapse': !sidebar.opened }">
      <div class="logo-container">
        <h1 class="logo-title">毕业设计管理系统</h1>
      </div>
      
      <el-scrollbar>
        <el-menu
          :default-active="activeMenu"
          :collapse="!sidebar.opened"
          :unique-opened="true"
          :collapse-transition="false"
          mode="vertical"
          router
        >
          <el-menu-item index="/">
            <el-icon><House /></el-icon>
            <template #title>首页</template>
          </el-menu-item>
          
          <el-menu-item 
            v-if="isAdmin || isTeacher" 
            index="/topics"
          >
            <el-icon><Document /></el-icon>
            <template #title>论文题目管理</template>
          </el-menu-item>
          
          <el-menu-item index="/selections">
            <el-icon><Select /></el-icon>
            <template #title>选题管理</template>
          </el-menu-item>
          
          <el-menu-item index="/task-books">
            <el-icon><Notebook /></el-icon>
            <template #title>任务书管理</template>
          </el-menu-item>
          
          <el-menu-item index="/proposal-reports">
            <el-icon><Document /></el-icon>
            <template #title>开题报告管理</template>
          </el-menu-item>
          
          <el-menu-item index="/guidance-records">
            <el-icon><ChatDotRound /></el-icon>
            <template #title>指导记录管理</template>
          </el-menu-item>
          
          <el-menu-item index="/thesis-documents">
            <el-icon><Files /></el-icon>
            <template #title>论文文档管理</template>
          </el-menu-item>
          
          <el-menu-item 
            v-if="isAdmin" 
            index="/users"
          >
            <el-icon><User /></el-icon>
            <template #title>用户管理</template>
          </el-menu-item>
        </el-menu>
      </el-scrollbar>
    </div>
    
    <!-- 主容器 -->
    <div class="main-container">
      <!-- 头部 -->
      <div class="navbar">
        <div class="left">
          <el-icon 
            class="hamburger" 
            @click="toggleSidebar"
          >
            <Fold v-if="sidebar.opened" />
            <Expand v-else />
          </el-icon>
        </div>
        
        <div class="right">
          <el-dropdown trigger="click">
            <div class="avatar-container">
              <el-avatar :size="32" :src="user.avatar">
                {{ user.real_name?.charAt(0) }}
              </el-avatar>
              <span class="user-name">{{ user.real_name }}</span>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="handleLogout">
                  <el-icon><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      
      <!-- 内容区 -->
      <div class="app-main">
        <router-view v-slot="{ Component }">
          <transition name="fade-transform" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '../store'
import { useAppStore } from '../store'
import { ElMessageBox } from 'element-plus'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()
const appStore = useAppStore()

const sidebar = computed(() => appStore.sidebar)
const device = computed(() => appStore.device)
const user = computed(() => userStore.user)
const isAdmin = computed(() => userStore.isAdmin)
const isTeacher = computed(() => userStore.isTeacher)

const activeMenu = computed(() => route.path)

const toggleSidebar = () => {
  appStore.toggleSidebar()
}

const handleLogout = async () => {
  try {
    await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      type: 'warning'
    })
    userStore.logout()
    router.push('/login')
  } catch {
    // 用户取消操作
  }
}
</script>

<style scoped>
.app-wrapper {
  position: relative;
  height: 100vh;
  width: 100%;
}

.sidebar-container {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 210px;
  background-color: #304156;
  transition: width 0.28s;
  z-index: 1001;
  overflow: hidden;
}

.sidebar-container.is-collapse {
  width: 64px;
}

.logo-container {
  height: 60px;
  padding: 10px 0;
  text-align: center;
  overflow: hidden;
}

.logo-title {
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  margin: 0;
  white-space: nowrap;
}

.main-container {
  min-height: 100%;
  margin-left: 210px;
  position: relative;
  transition: margin-left 0.28s;
}

.sidebar-container.is-collapse + .main-container {
  margin-left: 64px;
}

.navbar {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  background-color: #fff;
}

.hamburger {
  font-size: 20px;
  cursor: pointer;
}

.avatar-container {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.user-name {
  margin-left: 8px;
  font-size: 14px;
}

.app-main {
  padding: 20px;
  min-height: calc(100vh - 60px);
  background-color: #f0f2f5;
}

/* 过渡动画 */
.fade-transform-enter-active,
.fade-transform-leave-active {
  transition: all 0.3s;
}

.fade-transform-enter-from {
  opacity: 0;
  transform: translateX(-30px);
}

.fade-transform-leave-to {
  opacity: 0;
  transform: translateX(30px);
}

/* 移动端适配 */
.mobile .sidebar-container {
  transform: translateX(-210px);
}

.mobile .sidebar-container.is-collapse {
  transform: translateX(0);
}

.mobile .main-container {
  margin-left: 0;
}
</style> 